<?php

namespace app\home\controller;

// use app\common\controller\AdminController;
// use app\common\services\annotation\ControllerAnnotation;
// use app\common\services\annotation\NodeAnnotation;
use support\Request; //请求类
use support\Response; //请求类
use think\facade\Db; //数据库类
use app\common\traits\JumpTrait;
use app\common\DnfmailController;
/**
 * @ControllerAnnotation(title="group_user")
 */
class IndexController
{
	use JumpTrait;
	
	protected $noNeedLogin = ['index','info','bczm','open'];
    
    public function index(Request $request){
        if(sysconfig('home','hstatus')=="0"){
            return response('', 503);
        } 
        $bannerls = DB::name('home_banner')->select();
        $txtlist = DB::name('home_text')->select();
	    return view('/home/index/index',['bannerlist'=>$bannerls,'txtlist'=>$txtlist]);
	}
	
    public function bczm(Request $request){
        
	    if($request->get('type') == "1"){
            $shortcutContent = '[InternetShortcut]
            URL=http://'.$request->host().'/
            IDList=
            HotKey=0
            IconFile=http://'.$request->host().'/favicon.ico
            IconIndex=0
            ';
            $shortcutName = sysconfig('home','htitle').'.url';
	    }else{
            $shortcutContent = '[InternetShortcut]
            URL=http://'.$request->host().'/home/open
            IDList=
            HotKey=0
            IconFile=http://'.$request->host().'/favicon.ico
            IconIndex=0
            ';
            $shortcutName = sysconfig('im','title').'.url';
	    }
        
        return response($shortcutContent, 200, [
            'Content-Type' => 'application/octet-stream','Content-Disposition' => 'attachment; filename="' . $shortcutName . '"'
        ]);
    }
	public function open() {
        // window.open('/chat','_blank','width=540,height=960,menubar=no,toolbar=no,status=no,scrollbars=yes,titlebar=no') 
        return("
        <a onclick='opsen()'>如果被浏览器拦截,请手动允许或点我弹出</a>
        <script> 
            function opsen(){
                window.open('/chat','_blank','width=540,height=960,menubar=no,toolbar=no,status=no,scrollbars=yes,titlebar=no') 
            }
            setTimeout(function() {
                window.open('/chat','_blank','width=540,height=960,menubar=no,toolbar=no,status=no,scrollbars=yes,titlebar=no') 
            }, 500);
            setTimeout(function() {
                window.close();
            }, 5000);
        </script>");
        exit;
    }
	public function info(Request $request)
	{
	    if(!$request->get('id')){return $this->error('参数错误');}
        $txtlist = DB::name('home_text')->where(['id'=>$request->get('id')])->find();
		return view('home/index/info',['txtinfo'=>$txtlist]);
	}
	
	public function sign(Request $request){
	    
	    
	    

	    if($request->isAjax()){
	        if(DB::name('sign_list')->where(['uid'=>session('uid'),'time'=>date('Y-m-d')])->find()){
	            return $this->error("你今天已经签到过了");
	        }else{
	            DB::name('sign_list')->insert(['uid'=>session('uid'),'time'=>date('Y-m-d')]);
	            
	            $qdinfo = DB::name('sign_config')->where('id',$request->post('id'))->find();
	            $wuplist = $qdinfo['content'];
	            $lines = preg_split('/\r\n|\r|\n/', $wuplist);
	            $i = 0;
	            $dnfmailController = new DnfmailController();
	            foreach ($lines as $line) {
                    //分割每一行
                    $i++;
                    $ls = explode('|', $line);
                    if(count($ls) == "8"){
                        $dnfmailController->senddnfmail($request->post('noid'),$ls[0],$ls[6],$ls[1],$ls[2],$ls[7],$ls[5],$ls[3],$ls[4],'这是你的['.$qdinfo['name'].']奖励,祝您游戏愉快!','签到奖励-'.$i,session('name'),$request->post('name'));
                    }
                }
	            return $this->success("签到成功,奖励请小退查看");
	        }
	    }else{
            // 获取当前日期
            $currentDate = date('Y-m-d');
            $signinStatus = [];
            // 查询最近六天的签到记录
            $start_date = $currentDate;
            for ($i = 6; $i >= 0; $i--) {
                $date = date('Y-m-d', strtotime("-$i day", strtotime($currentDate)));
                $record = DB::name('sign_list')->where(['uid'=>session('uid'),'time'=>$date])->find();
                if ($record) {
                    // 如果有签到记录，则从该日期开始计算七天签到状态
                    $start_date = $date;
                    break;
                }
            }
            // 生成七天签到状态
            for ($i = 0; $i < 7; $i++) {
                $date = date('Y-m-d', strtotime("+$i day", strtotime($start_date)));
                if ($date < $currentDate) {
                    // 检查历史日期
                    $record = DB::name('sign_list')->where(['uid'=>session('uid'),'time'=>$date])->find();
                    if ($record) {
                        $signinStatus[$date] = '已签';
                    } else {
                        $signinStatus[$date] = '漏签';
                    }
                } elseif ($date == $currentDate) {
                    $signinStatus[$date] = '签到';
                    $record = DB::name('sign_list')->where(['uid'=>session('uid'),'time'=>$date])->find();
                    if ($record) {
                        $signinStatus[$date] = '已签';
                    }
                } else {
                    $signinStatus[$date] = '待签';
                }
            }
            foreach ($signinStatus as $date => $status) {
                $color = "#898989";
                if($status == '签到'){
                    $color = "#019925";
                }elseif($status == '已签'){
                    $color = "#5a628b";
                }elseif($status == '待签'){
                    $color = "#898989";
                }elseif($status == '漏签'){
                    $color = "#890101";
                }
                $newArray[] = [
                    'status' => $status,
                    'date' => $date,
                    'color' => $color
                ];
            }
            $qd =DB::name('sign_config')->select();

            return view('home/index/sign',['qdlist'=>$newArray,'qd'=>$qd]);
	    }
	}
	
	public function getnoidlist(Request $request){
	    $dnfmailController = new DnfmailController();
	    $uid = $dnfmailController->getuid(session('name'));
	    if($uid == "-1"){
	        return $this->error('未在游戏中找到账号');
	    }
	    $list = $dnfmailController->getnolist($uid);
	    if(count($list) == 0){
	        return $this->error('未在游戏中找到角色');
	    }
	    return $this->success('success',$list->toArray());
	}
	
	public function cdk(){
	    return view('home/index/cdk');
	}
	
	public function inscdk(Request $request){
        $cdkinfo = DB::name('cdk_list')->where('cdk',$request->post('cdk'))->find();
        if(!$cdkinfo){
            return $this->error('兑换码不存在');
        }
        if($cdkinfo['status'] == "1"){
            return $this->error('兑换码已被使用');
        }
        if(time() > $cdkinfo['maxtime']){
            return $this->error('兑换码已过期');
        }
        if(time() < $cdkinfo['mintime']){
            return $this->error('活动未开始!该兑换码开始兑换时间:'.date("Y-m-d H:i:s", $cdkinfo['mintime']));
        }
        //发送邮件
        $lines = preg_split('/\r\n|\r|\n/', $cdkinfo['content']);
        $i = 0;
        $dnfmailController = new DnfmailController();
        foreach ($lines as $line) {
            //分割每一行
            $i++;
            $ls = explode('|', $line);
            if(count($ls) == "10"){
                /*角色id,物品id,数量,红字类型1为体力，2为精神，3为力量，4为智力,红字数值,金币,封装,锻造,强化,说明,标题,用户账号,角色账号,是否补发*/
                $dnfmailController->senddnfmail($request->post('noid'),$ls[0],$ls[6],$ls[1],$ls[2],$ls[7],$ls[5],$ls[3],$ls[4],$ls[9],$ls[8],session('name'),$request->post('name'));
            }
        }
        DB::name('cdk_list')->where('cdk',$request->post('cdk'))->update(['status'=>1,'addtime'=>time(),'noid'=>$request->post('noid'),'account'=>session('name'),'gamename'=>$request->post('name')]);
        return $this->success($cdkinfo['name']."兑换成功");
	}
	
	public function invite(Request $request){
	    if(sysconfig('invite','istatus') == '0'){
	        return $this->error('邀请功能已被关闭');
	    }
	    $info = DB::name('user')->where('id',session('uid'))->find();
	    return view('home/index/invite',['info'=>$info]);
	}
	
	public function yqoncli(Request $request){
	    $info = DB::name('user')->where('id',session('uid'))->find();
	    if($info['invitecs'] < 1){
	        return $this->error('兑换次数不足');
	    }
	    if(!$request->post('noid') or !$request->post('name')){
	        return $this->error('参数异常');
	    }
	    $dnfmailController = new DnfmailController();
	    if(sysconfig('invite','invitename1') != "#"){
	        $ls = explode('|', sysconfig('invite','invitecon1'));
            if(count($ls) == "10"){
                /*角色id,物品id,数量,红字类型1为体力，2为精神，3为力量，4为智力,红字数值,金币,封装,锻造,强化,说明,标题,用户账号,角色账号,是否补发*/
                $dnfmailController->senddnfmail($request->post('noid'),$ls[0],$ls[6],$ls[1],$ls[2],$ls[7],$ls[5],$ls[3],$ls[4],$ls[9],$ls[8],session('name'),$request->post('name'));
            }
	    }
	    if(sysconfig('invite','invitename2') != "#"){
	        $ls = explode('|', sysconfig('invite','invitecon2'));
            if(count($ls) == "10"){
                /*角色id,物品id,数量,红字类型1为体力，2为精神，3为力量，4为智力,红字数值,金币,封装,锻造,强化,说明,标题,用户账号,角色账号,是否补发*/
                $dnfmailController->senddnfmail($request->post('noid'),$ls[0],$ls[6],$ls[1],$ls[2],$ls[7],$ls[5],$ls[3],$ls[4],$ls[9],$ls[8],session('name'),$request->post('name'));
            }
	    }
	    if(sysconfig('invite','invitename3') != "#"){
	        $ls = explode('|', sysconfig('invite','invitecon3'));
            if(count($ls) == "10"){
                /*角色id,物品id,数量,红字类型1为体力，2为精神，3为力量，4为智力,红字数值,金币,封装,锻造,强化,说明,标题,用户账号,角色账号,是否补发*/
                $dnfmailController->senddnfmail($request->post('noid'),$ls[0],$ls[6],$ls[1],$ls[2],$ls[7],$ls[5],$ls[3],$ls[4],$ls[9],$ls[8],session('name'),$request->post('name'));
            }
	    }
	    if(sysconfig('invite','invitename4') != "#"){
	        $ls = explode('|', sysconfig('invite','invitecon4'));
            if(count($ls) == "10"){
                /*角色id,物品id,数量,红字类型1为体力，2为精神，3为力量，4为智力,红字数值,金币,封装,锻造,强化,说明,标题,用户账号,角色账号,是否补发*/
                $dnfmailController->senddnfmail($request->post('noid'),$ls[0],$ls[6],$ls[1],$ls[2],$ls[7],$ls[5],$ls[3],$ls[4],$ls[9],$ls[8],session('name'),$request->post('name'));
            }
	    }
	    if(sysconfig('invite','invitename5') != "#"){
	        $ls = explode('|', sysconfig('invite','invitecon5'));
            if(count($ls) == "10"){
                /*角色id,物品id,数量,红字类型1为体力，2为精神，3为力量，4为智力,红字数值,金币,封装,锻造,强化,说明,标题,用户账号,角色账号,是否补发*/
                $dnfmailController->senddnfmail($request->post('noid'),$ls[0],$ls[6],$ls[1],$ls[2],$ls[7],$ls[5],$ls[3],$ls[4],$ls[9],$ls[8],session('name'),$request->post('name'));
            }
	    }
	    DB::name('user')->where('id',session('uid'))->setDec('invitecs',1);
	    return $this->success('操作完成');
	}
}
